$(function () {
    var token = localStorage.token;
    if(!token) {
        location.href = "../loginAndReg/login.html"
    }
    new FastClick(document.body);
    //展开和收起信息
    $(".msgList").on("click", ".msgItem", function () {
        $(this).find(".msgDetail").css("display", "block");
        $(this).find(".collapse").css("display", "block");
        var height = $(this).parent().parent().height();
        $(this).parent().find(".delBtn").height(height);
        $(this).parent().find(".delBtn").css("line-height", height + "px");
    });
    $(".msgList").on("click", ".collapse", function (event) {
        event.stopPropagation();
        $(".msgDetail").css("display", "-webkit-box");
        $(this).attr("style", "display:none");
        var height = $(this).parent().parent().height() + parseFloat($(this).parent().parent().css("padding-top"));
        $(this).parent().parent().parent().find(".delBtn").height(height);
        $(this).parent().parent().parent().find(".delBtn").css("line-height", height + "px");
    });
    //获取消息列表
    var flag = false;
    var pageNum = 1;
    function loadMsg() {
        if (flag) return false;
        $.ajax({
            type: "get",
            url: domain.test + "/getmsg",
            // url: domain.online + "/msg",
            beforeSend: function (xhr) {
                flag = true;
                xhr.setRequestHeader("token", token);
            },
            success: function (response) {
                if (response.data.rtnCode === 200) {
                // if (response.rtnCode === 200) {
                    var msgDetail = response.data.data;
                    // var msgDetail = response.data;
                    if (pageNum === 1) {
                        $(".msgList").html(template("msgList", { oderList: msgDetail.list }));
                    } else {
                        $(".msgList").append(template("msgList", { oderList: msgDetail.list }));
                    }
                    //设置删除按钮的高度
                    var height = $(".msgBox").height();
                    $(".delBtn").height(height);
                    $(".delBtn").css("line-height", height + "px");
                    pullmaster.pullDownSuccess();       //刷新成功
                    if (msgDetail.list.length < 10 || msgDetail.list.length === 0) {
                        pullmaster.pullUpDone();        //到底啦
                        $(".bb_pull-up").css({ "display": "none" });
                        if(msgDetail.list.length!==0) {
                            $(".end").css("display", "block");
                        }
                    }
                    ++pageNum;
                    flag = false;
                }
                if (response.data.rtnCode === 401) {
                // if (response.rtnCode === 401) {
                    var outTimeCode = response.data.rtnCode;
                    // var outTimeCode = response.rtnCode;
                    location.href = "../../index.html?outTimeCode=" + outTimeCode;
                }
            }
        });
    }
    var pullmaster = new Pull($('.msgList'), {
        onPullUp: function () {
            loadMsg();
        },
        onPullDown: function () {
            pageNum = 1;
            loadMsg();
        }
    });
    //滑动显示删除按钮
    var startX, animateX, endX;
    $(".msgList").on("touchstart", ".msgItemBox", function (e) {
        startX = e.changedTouches[0].clientX;
        console.log(startX);
    });
    $(".msgList").on("touchmove", ".msgItemBox", function (e) {
        animateX = e.changedTouches[0].clientX;
    });
    $(".msgList").on("touchend", ".msgItemBox", function (e) {
        endX = e.changedTouches[0].clientX;
        if (Math.abs(startX - endX) > 10) {
            var temp = parseFloat($(".delBtn").css("width"));
            if (startX - endX > 0) {
                var left = parseFloat($(this).css("left"));
                if (left === 0) {
                    $(this).animate({
                        "left": -temp
                    }, 300);
                }

            }
            if (startX - endX < 0) {
                var left = parseFloat($(this).css("left"));
                if (left === -temp) {
                    $(this).animate({
                        "left": 0
                    }, 300);
                }
            }
        }
    });
    //删除消息
    $(".msgList").on("click",".delBtn",function() {
        var id = $(this).attr("data-id");
        $.ajax({
            type: "get",
            url: domain.test + "/delmsg",
            data: {id:id},
            beforeSend: function(xhr) {
                xhr.setRequestHeader("token",token);
            },
            success: function (response) {
                if(response.data.rtnCode===200) {
                    location.reload();
                }
            }
        });
    });
});